/**
 * 
 */
package cn.owen.util;

import java.io.IOException;

import javax.crypto.Mac;

import com.alibaba.fastjson.JSONException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**七牛上传工具类
 * @author Burning
 * @email wenbao163.good@163.com
 * @date 2016年8月4日下午5:39:32
 * 
 */
public class QiniuUtil {
	//设置好账号的ACCESS_KEY和SECRET_KEY
	  String ACCESS_KEY = "";
	  String SECRET_KEY = "";
	  //要上传的空间
	  String bucketname = "";
	  //上传到七牛后保存的文件名
	  String key = "my-java.png";
	  //上传文件的路径
	  String FilePath = "/.../...";
	  //密钥配置
	  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	  //创建上传对象
	  UploadManager uploadManager = new UploadManager();
	  //简单上传，使用默认策略，只需要设置上传的空间名就可以了
	  public String getUpToken(){
	      return auth.uploadToken(bucketname);
	  }

	  public String upload(String filePath,String fileName) throws IOException{
	    try {
	    	//调用put方法上传
	    	Response res = uploadManager.put(filePath, fileName, getUpToken());
	      	//打印返回的信息
	      	return res.bodyString(); 
	     } catch (QiniuException e) {
	          Response r = e.response;
			  return null;
	      }       
	  }

	  public static void main(String args[]) throws IOException{  
		  String a="C:\\Users\\wangwenbao\\AppData\\Local\\Temp\\tomcat-docbase.7903396186321345643.8200\\upload\\user\\20160804170246709.jpg";
		  new QiniuUtil().upload(a,null);
	  }
}
